/**
 * 
 */
package se.cirrus.base.com;

import se.cirrus.base.CirrusVersion;
import se.cirrus.base.struct.Content;

/**
 * @author Winquman
 *
 */
public class BasicIPMessage implements Message {
	public static final int TYPE_SYSTEM = 10;
	public static final int TYPE_CONTENT = 100;
	public static final int TYPE_CONTENT_CHAT = 110;
	public static final int TYPE_HEARTBEAT = 30;
	
	private final CirrusVersion version;
	private final int time;
	private final int source;
	private final Content content;
	private final int type;

	/**
	 * 
	 */
	public BasicIPMessage(String serializedMessage) {
		String head;
		String body;
		
		String s[];
		
		s = serializedMessage.split("$$");
		head = s[0];
		body = s[1];
		

		version = parseVersion(head);
		time = parseTime(head);
		source = parseSource(head);
		type = parseType(head);
		
		content = new Content(body);
	}
	
	private int parseContentType(String s) {
		String ss[] = s.split("/");
		
		return Integer.parseInt(ss[4]);
	}

	/**
	 * 
	 * @param v
	 * @param localTime
	 * @param localID
	 * @param c
	 */
	public BasicIPMessage(CirrusVersion v, int localTime, int localID, Content c) {
		version = v;
		time = localTime;
		source = localID;
		content = c;
		type = 20;
	}
	
	
	public BasicIPMessage(CirrusVersion v, int localTime, int localID, Content c, int messageType) {
		version = v;
		time = localTime;
		source = localID;
		content = c;
		type = messageType;
		
	}
	
	
	private int parseType(String s) {
		String ss[] = s.split("/");
		
		return Integer.parseInt(ss[3]);
	}
	
	
	private int parseTime(String s) {
		String ss[] = s.split("/");
		
		return Integer.parseInt(ss[1]);
	}
	
	
	private int parseSource(String s) {
		String ss[] = s.split("/");
		
		return Integer.parseInt(ss[2]);
	}
	
	private CirrusVersion parseVersion(String s) {
		String ss[] = s.split("/");
		
		String sss[] = ss[0].split(".");
		return new CirrusVersion(Integer.parseInt(sss[0]), Integer.parseInt(sss[1]), Integer.parseInt(sss[2]));
	}

	/* (non-Javadoc)
	 * @see se.cirrus.base.com.Message#getTime()
	 */
	@Override
	public int getTime() {
		return time;
	}

	/* (non-Javadoc)
	 * @see se.cirrus.base.com.Message#getSource()
	 */
	@Override
	public int getSource() {
		return source;
	}

	/* (non-Javadoc)
	 * @see se.cirrus.base.com.Message#compareTo(se.cirrus.base.com.Message)
	 */
	@Override
	public int compareTo(Message m) {
		return time - m.getTime();
	}

	/* (non-Javadoc)
	 * @see se.cirrus.base.com.Message#getMessageContent()
	 */
	@Override
	public Content getMessageContent() {
		
		return content;
	}

	@Override
	public boolean systemMessage() {
		
		return (type == TYPE_SYSTEM);
	}
	
	public boolean hasContent() {
		
		return (type == TYPE_CONTENT);
	}
	
	
	public String toString() {
		return "" + version + "/" + time + "/" + source + "/" + type + "$$" + content; 
	}


}
